package com.forcedotcom.components
{
	import com.forcedotcom.model.PostListData;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.core.IUID;
	
	import spark.components.supportClasses.ItemRenderer;
	
	public class PostItemDataArrayCollection extends ArrayCollection
	{
		
		public var plData:PostListData;
		
		public function PostItemDataArrayCollection()
		{
			super();
		}

		override public function getItemIndex(item:Object):int
		{
			var i:int;
			
			if (localIndex && super.sort)
			{
				var startIndex:int = sort.findItem(localIndex, item, Sort.FIRST_INDEX_MODE);
				if (startIndex == -1)
					return -1;
				
				var endIndex:int = sort.findItem(localIndex, item, Sort.LAST_INDEX_MODE);
				for (i = startIndex; i <= endIndex; i++)
				{
					if (localIndex[i].uid == item.uid)
						return i;
				}
				
				return -1;
			}
			else if (localIndex && filterFunction != null)
			{
				var len:int = localIndex.length;
				for (i = 0; i < len; i++)
				{
					if (localIndex[i] == item)
						return i;
				}
				
				return -1;
			}
			
			// fallback
			return myGetItemIndex(item);
		}
	
		private function myGetItemIndex(item:Object):int
		{
			var n:int = source.length;
			for (var i:int = 0; i < n; i++)
			{
				if (source[i].uid === item.uid)
					return i;
			}
			
			return -1;           
		}
		
	}
}